package com.pickgliss.ui.controls
{
	import com.pickgliss.geom.InnerRectangle;
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.ui.image.Image;
	import com.pickgliss.utils.ClassUtils;
	import com.pickgliss.utils.DisplayUtils;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Rectangle;

	public class IconButton extends TextButton
	{
		public static const P_icon:String = "icon";
		public static const P_iconInnerRect:String = "iconInnerRect";

		public function IconButton()
		{
			super();
		}
		
		public var iconFrameChange:Boolean = true;
		protected var _icon:DisplayObject;
		protected var _iconInnerRect:InnerRectangle;
		protected var _iconInnerRectString:String;
		protected var _iconStyle:String;
		
		override public function dispose():void
		{
			if(_icon)ObjectUtils.disposeObject(_icon);
			_icon = null;
			super.dispose();
		}
		/**
		 * 
		 * @param display 图标
		 * 
		 */		
		public function get icon():DisplayObject
		{
			return _icon;
		}
		public function set icon(display:DisplayObject):void
		{
			if(_icon == display)return;
			ObjectUtils.disposeObject(_icon);
			_icon = display;
			onPropertiesChanged(P_icon);
		}
		/**
		 * 
		 * @param value 设置图标的InnerRectangle
		 * 具体请看InnerRectangle
		 * 
		 */		
		public function set iconInnerRectString(value:String):void
		{
			if(_iconInnerRectString == value)return;
			_iconInnerRectString = value;
			_iconInnerRect = ClassUtils.CreatInstance(ClassUtils.INNERRECTANGLE,ComponentFactory.parasArgs(_iconInnerRectString));
			onPropertiesChanged(P_iconInnerRect);
		}
		public function get iconInnerRectString():String
		{
			return _iconInnerRectString;
		}
		public function set iconStyle(stylename:String):void
		{
			if(_iconStyle == stylename)return;
			_iconStyle = stylename;
			icon = ComponentFactory.Instance.creat(_iconStyle);
		}
		public function get iconStyle():String
		{
			return _iconStyle;
		}
		override protected function addChildren():void
		{
			super.addChildren();
			if(_icon)addChild(_icon);
		}
		
		override protected function onProppertiesUpdate():void
		{
			super.onProppertiesUpdate();
			if(_changedPropeties[Component.P_width] || _changedPropeties[Component.P_height] || _changedPropeties[P_iconInnerRect] ||_changedPropeties[P_icon])
			{
				updateIconPos();
			}
		}

		protected function updateIconPos():void
		{
			if(_icon && _iconInnerRect)
			{
				DisplayUtils.layoutDisplayWithInnerRect(_icon,_iconInnerRect,_width,_height);
			}
		}
		
		override public function setFrame(frameIndex:int):void
		{
			super.setFrame(frameIndex);
			if(iconFrameChange) DisplayUtils.setFrame(_icon,frameIndex);
		}
	}
}